Subject: Re: allocating 0 bytes with malloc()?
From: [email-masked] (Larry Weiss)
Date: Tue, 1 Mar 1994 22:28:26 GMT
Newsgroups: comp.lang.c,comp.std.c

[email-masked] (Bill Mitchell) writes:

>in comp.lang.c, [email-masked] (Conor O'Neill) said:
>>However, unfortunately the issue is now moot. ANSI C says that
>>malloc(0) is one of those funny little wrinkles, and as such,
>>programmers must code around it.

>Where?  Section 4.10.3.3 says, in its entirety:
>    4.10.3.3 The malloc Function
>    
>    Synopsis
>        #include <stdlib.h>
>        void *malloc(size_t size);
>    
>    Description
>        The malloc function allocates space for an object whose size is
>        specified by size and whose value is indeterminate.
>    
>    Returns
>        The malloc function returns either a null pointer or a pointer to
>        the allocated space.

There is additional language in the enclosing section 7.10.3 (ISO numbering, 
your 4.10.3):

7.10.3 Memory management functions

   The order and contiguity of storage allocated by sucessive calls to the
calloc, malloc, and realloc functions is unspecified.  The pointer returned
if the allocation succeeds is suitably aligned so that it may be assigned
to a pointer to any type of object and then used to access such an object
or an array of such objects in the space allocated (until the space is 
explicitly freed or reallocated).  Each such allocation shall yield a 
pointer to an object disjoint from any other object.  The pointer returned
points to the start (lowest byte address) of the allocated space.  If
the space cannot be allocated, a null pointer is returned.  If the size 
of the space requested is zero, the behavior is implementation-defined;
the value returned shall be either a null pointer or a unique pointer.
The value of a pointer that refers to freed space is indeterminate.

-- 
Larry Weiss, [email-masked]  
214/888-0471